<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>基本资料</span>
    </div>
    <!-- 表单 -->
    <el-form ref="userForm" :model="userForm" :rules="userFormRules" label-width="80px">
  <el-form-item label="登录名称" prop="username" >
    <el-input v-model="userForm.username" disabled></el-input>
  </el-form-item>
   <el-form-item label="用户昵称" prop="nickname">
    <el-input v-model="userForm.nickname"></el-input>
  </el-form-item>
   <el-form-item label="用户邮箱" prop="email">
    <el-input v-model="userForm.email"></el-input>
  </el-form-item>
  <el-form-item>
<el-button type="primary" @click="subForm">提交修改</el-button>
<el-button @click="$refs.userForm.resetFields()">重置</el-button>
  </el-form-item>
  </el-form>
  </el-card>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'UserInfo',
  data() {
    return {
      userForm: { username: '', nickname: '', email: '' },
      userFormRules: {
        nickname: [
          { required: true, megssage: '请输入用户名', trigger: ' blur ' },
          { pattern: /^\S{1,10}$/, megssage: '昵称为1~10的为非空字符串', trigger: ' blur ' }],
        email: [
          { required: true, megssage: '请输入邮箱', trigger: ' blur ' },
          { type: 'email', megssage: '请输入正确邮箱', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapState('user', ['userinfo'])
  },
  created() {
    this.userForm = { ...this.userinfo }
  },
  methods: {
    subForm() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.get('/my/userinfo', this.userForm)
        if (res.code !== 0) return this.$megssage.error(res.megssage)
        this.$message.success('更新用户信息成功！')
        this.$store.dispatch('user/getUserInfo', res.data)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
